<script setup lang="ts">
import { inject } from 'vue'
import AddOrConfirmForm from '@renderer/views/patients/components/modal/components/AddOrConfirmForm.vue'
import { useControlGlobalModal } from '@renderer/layout/hooks/useControlGlobalModal'
import { GlobalNotification } from '@renderer/components/Toast/GlobalNotification'

const { showChooseScanModeRef } = useControlGlobalModal()
const { addPatientVisibleRef, modifiedPatientRef, createPatientInfo, getPatientList } = inject('patients') as any

const handleClose = () => {
  addPatientVisibleRef.value = false
}

const handleSave = async () => {
  try {
    await createPatientInfo(modifiedPatientRef.value, 'add')
    await getPatientList()
    handleClose()
    GlobalNotification({
      title: 'tips',
      message: 'add Successfully!',
      type: 'success'
    })
  } catch (error) {
    GlobalNotification({
      title: 'Error',
      message: error,
      type: 'error'
    })
  }
}

const createAndScan = async () => {
  try {
    if (modifiedPatientRef.value.patientId === '') {
      modifiedPatientRef.value = await createPatientInfo(modifiedPatientRef.value, 'add')
    }
    showChooseScanModeRef.value = true
  } catch (e) {
    console.log(e)
  }
}
</script>

<template>
  <el-dialog
    title="添加患者信息"
    destroy-on-close
    width="900"
    :before-close="handleClose"
  >
    <template #default>
      <div class="max-h-[500px] overflow-auto">
        <add-or-confirm-form type="add" />
      </div>
    </template>
    <template #footer>
      <el-button @click="handleSave">保存</el-button>
      <el-button @click="handleClose">取消</el-button>

      <el-button type="primary" @click="createAndScan">保存并扫描</el-button>
    </template>
  </el-dialog>
</template>

<style scoped>

</style>
